<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/24
 * Time: 17:03
 * 帅帅吴
 */

namespace app\admin\model;


class RedPacket extends DataBase
{
    protected $name = 'red_packet';
    protected $autoWriteTimestamp = 'true';

    public function getIsMaxAttr($value)
    {
        $status = [0=>'不是',1=>'运气王'];
        return $status[$value];
    }
    public function getStatusAttr($value)
    {
        $status = [0=>'未领取',1=>'已领取',2=>'已过期',3=>'已退回'];
        return $status[$value];
    }

    public function getDataList()
    {

        $page_config = array(
            'page' => input('offset', 1),
            'list_rows' => input('limit', 10),
        );
        $where = [];
        if(!empty(input('value'))){
            $key = input('key');
            $value = input('value');
            if($key=='id' ){
                $where[] = ['a.'.$key,'like','%'.$value.'%'];
            }
            if($key=='send_user_id'){
                $where[] = ['b.nickname','like','%'.$value.'%'];
            }
            if($key=='recive_user_id'){
                $where[]= ['c.nickname','like','%'.$value.'%'];
            }
            if($key=='r_name'){
                $where[] = ['d.'.$key,'like','%'.$value.'%'];
            }
        }
        if(!empty(input('start_time')) && !empty(input('end_time'))){
            $start = strtotime(input('start_time'));
            $end = strtotime(input('end_time'));
            $where[] = ['a.create_time','>=',$start];
            $where[] = ['a.create_time','<',$end];
        }
        $order = input('sort', 'a.id') . " " . input('order', 'asc');


        return $this
            ->alias('a')
            ->field('a.id,b.nickname as send_nickname,c.nickname as recive_nickname,d.r_name as room_name,a.diamonds,a.is_max,a.number,a.create_time,a.status')
            ->join('users b','a.send_user_id = b.id','left')
            ->join('users c','a.recive_user_id = c.id','left')
            ->join('rooms d','a.room_id = d.id','left')
            ->where($where)
            ->order($order)
            ->paginate($page_config);
    }

}